/**
 * Created by xiewei on 10/19/15.
 */
var hospital = {};
var imgDir = '../img/'
/**
 * 等级icon
 */
var levelImg = {
    'A': 'icon_a@2x.png',
    'B': 'icon_b@2x.png'
}
/**
 * 默认图片
 */
var dfltLogo = 'icon.png';
var dfltCover = 'cover.jpg';
var dfltDoc = 'icon.png';
/**
 * 填入医院详细数据
 * @param hospital
 */
var fillHospitalData = function(hospital){
    var id = hospital.id;
    var coverImg = $('.coverImg');
    if(hospital.coverImg=='' || hospital.coverImg==null){
        coverImg.children('img').attr('src',imgDir+dfltCover);
    }else{
        coverImg.children('img').attr('src',imgDir+id+'/'+hospital.coverImg);
    }
    coverImg.find('time').text(hospital.time.join('-'));
    var iconImg = $('.logoImg img');
    if(hospital.logoImg=='' || hospital.logoImg==null){
        $(iconImg[0]).attr('src',imgDir+dfltLogo);
    }else{
        $(iconImg[0]).attr('src',imgDir+id+'/'+hospital.logoImg);
    }
    $(iconImg[1]).attr('src',imgDir+levelImg[hospital.level]);
    $('#name').text(hospital.name);
    $('head title').text('小狗在家合作医院-'+hospital.name);
    $('#address').text(hospital.address.join(' '));
    $('#tel').text(hospital.tel);

    $('.app_hos_intro p:last').text(hospital.intro);
    $('.app_hos_character p:last').text(hospital.character);
    var doclist = $('.app_hos_doc ul');
    var docs = hospital.docList;
    for(i in docs){
        var img = $('<img>');
        if(docs[i].pic=='' || docs[i].pic==null){
            img.attr('src',imgDir+dfltDoc);
        }else{
            img.attr('src',imgDir+id+'/'+docs[i].pic);
        }
        var h2 = $('<h2>').text(docs[i].name);
        var span = $('<span>').text(docs[i].post);
        var p = $('<p>').text(docs[i].intro);
        var div = $('<div>').append(h2,span,p);
        $('<li>').append(img,div).appendTo(doclist);
    }

    var equiplist = $('.app_hos_equip ul');
    var equips = hospital.equipList;
    for(i in equips){
        var h2 = $('<h2>').text(equips[i].name);
        var p = $('<p>').text(equips[i].detail);
        $('<li>').append(h2,p).appendTo(equiplist);
    }

    var piclist = $('.app_hos_img ul');
    var pics = hospital.picList;
    for(i in pics){
        var img = $('<img>').attr('src',imgDir+id+'/'+pics[i]);
        $('<li>').append(img).appendTo(piclist);
    }
}

/**
 * 图片查看
 */
var banFlag = false;
var doc = document;
var stop = null;
$(function(){
    hospital = JSON.parse(localStorage.selectedHospital);
    fillHospitalData(hospital);
    var scrWidth = 185*hospital.picList.length;
    $('#scroller').css({width:scrWidth});

    //var imgList = $('.app_hos_img img');
    //imgList.on('click',function(i){
    //    showImg(imgList,i+1);
    //});

    //$("#slides").click(function(){
    //    //关闭
    //    $("#shade_cover").animate({
    //        opacity: "0"
    //    },300);
    //    setTimeout("$('#shade_cover').hide()",200);
    //    setTimeout("$('#slides').empty()",300);
    //    restartScroll();
    //});
});
/**
 * 查看大图
 * @param imgList
 * @param i
 */
function showImg(imgList,i){
    stop = doc.documentElement.scrollTop == 0 ? doc.body.scrollTop : doc.documentElement.scrollTop;
    //显示遮罩
    $("#shade_cover").css({
        height: screen.height
    }).show().animate({
        opacity: "1"
    },300);
    //拼图片展示相关节点
    var slides = $("#slides");
    imgList.each(function(){
        var img = $("<img>").attr("src",imgDir+$(this).attr("src")).hide();
        //图片定位
        var height = screen.height-100;
        if(img[0].height/img[0].width > height/screen.width){
            img.css({
                height: height,
                width: height/img[0].height*img[0].width
            });
        }else{
            var imgHeight = screen.width/img[0].width*img[0].height;
            img.css({
                width: screen.width,
                height: imgHeight,
                top : height/2-imgHeight/2
            });
        }
        slides.append(img);
    });
    slides.slidesjs({
        heigth: screen.height-50,
        start: i,
        navigation: false
    });
    slides.css("margin-top","25px");
    stopScroll();
}
/**
 * 禁止页面滚动
 */
var stopScroll = function(){
    banFlag = true;
    $("#shade_cover")[0].scrollTop = "0px";
    document.getElementById("shade_cover").scrollTop = "0px";
    document.documentElement.style.overflow = "hidden";
    document.body.style.overflow = "hidden";
    document.addEventListener("touchmove",function(e){
        if(banFlag){
            e.preventDefault;
            e.returnValue = false;
            e.stopPropagation();
            return false;
        }
    });
}
/**
 * 恢复页面滚动
 */
var restartScroll = function(){
    document.documentElement.scrollTop = stop;
    document.body.scrollTop = stop;
    document.documentElement.style.overflowY = "auto";
    document.body.style.overflowY = "auto";
    banFlag = false;
}
